package com.example.weather_forecast;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

/**
 * 在设备启动完成后自动重设所有已启用的通知
 */
public class BootCompletedReceiver extends BroadcastReceiver {

    private static final String PREFS_NAME = "WeatherNotificationSettings";
    private static final String KEY_ENABLE_NOTIFICATION = "enable_notification";
    private static final String KEY_DAILY_FORECAST = "daily_forecast";
    private static final String KEY_NOTIFICATION_TIME_HOUR = "notification_time_hour";
    private static final String KEY_NOTIFICATION_TIME_MINUTE = "notification_time_minute";
    private static final String KEY_SOUND = "sound";
    private static final String KEY_VIBRATION = "vibration";
    private static final String KEY_ENABLE_ALERT = "enable_alert";
    private static final String KEY_ALERT_RAIN = "alert_rain";
    private static final String KEY_ALERT_TYPHOON = "alert_typhoon";
    private static final String KEY_ALERT_HEAT = "alert_heat";
    private static final String KEY_ALERT_COLD = "alert_cold";

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction() != null && intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
            // 加载保存的设置
            SharedPreferences preferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
            boolean enableNotification = preferences.getBoolean(KEY_ENABLE_NOTIFICATION, true);

            if (enableNotification) {
                // 重新设置每日天气预报通知
                boolean dailyForecast = preferences.getBoolean(KEY_DAILY_FORECAST, true);
                int hour = preferences.getInt(KEY_NOTIFICATION_TIME_HOUR, 7);
                int minute = preferences.getInt(KEY_NOTIFICATION_TIME_MINUTE, 0);
                boolean sound = preferences.getBoolean(KEY_SOUND, true);
                boolean vibration = preferences.getBoolean(KEY_VIBRATION, true);

                if (dailyForecast) {
                    NotificationScheduler.scheduleDailyForecast(context, hour, minute, sound, vibration);
                }

                // 重新设置天气预警
                boolean enableAlert = preferences.getBoolean(KEY_ENABLE_ALERT, true);
                if (enableAlert) {
                    boolean alertRain = preferences.getBoolean(KEY_ALERT_RAIN, true);
                    boolean alertTyphoon = preferences.getBoolean(KEY_ALERT_TYPHOON, true);
                    boolean alertHeat = preferences.getBoolean(KEY_ALERT_HEAT, true);
                    boolean alertCold = preferences.getBoolean(KEY_ALERT_COLD, true);

                    WeatherAlertManager.setWeatherAlertSettings(
                            context,
                            alertRain,
                            alertTyphoon,
                            alertHeat,
                            alertCold,
                            sound,
                            vibration
                    );
                }
            }
        }
    }
}